import express from "express";
var moment = require("moment");
const router = express.Router();
router.post("/loginOrRegister", (req, res) => {
  let usersModel = require("../../models/users.js");
  usersModel.find({ user_name: req.body.name }, function(err, doc) {
    if (doc.length) {
      if (doc[0].password == req.body.password) {
        console.log("用户登录");
        delete doc[0].password;
        res.send({ errorCode: 0, data: doc[0] });
      } else {
        res.send({ errorCode: 1001,msg:'密码错误' });
      }
    } else {
      var newusers = new usersModel({
        user_name: req.body.name,
        user_token: Math.random(),
        user_dir: "./public/img/" +(req.body.password.slice(10, 15) +(Math.random() + "").split(".")[1]),
        // user_dir:'svip001',
        password: req.body.password,
        last_login_time: moment().format("YYYY-MM-DD HH:mm:ss")
      });
      newusers.save(function(err, doc) {
        if (err) {
          console.log(err);
          return res.send({ msg: "服务器异常", errorCode: 500 });
        } else {
          const fs = require("fs");
          fs.mkdir(
            "./public/img/" +(req.body.password.slice(10, 15) +(Math.random() + "").split(".")[1]),
            err => {
              if (err) return res.send({ errorCode: 1002,msg:'创建用户文件夹失败' });
            }
          );
          console.log("保存用户成功");
          res.send({ msg: "ok", errorCode: 0, data: doc });
        }
      });
    }
  });

  // let connectPgSql = require("../../libs/pg.js");
  // connectPgSql.query(
  //   "select * from users where user_name=$1",
  //   [req.body.name],
  //   function(err, data) {
  //     if (err) return console.log(err);
  //     if (data.rows.length) {
  //       if (req.body.password != data.rows[0].password)
  //         return res.send({ errorCode: 1000, msg: "密码错误！" });
  //       connectPgSql.query(
  //         "update users set last_login_time=now() where user_name=$1",
  //         [req.body.name],
  //         function(err, data2) {
  //           if (err) {
  //             res.send({ errorCode: 501 });
  //             return console.log(err);
  //           }
  //           delete data.rows[0].password;
  //           res.send({ errorCode: 0, data: data.rows[0] });
  //         }
  //       );
  //     } else {
  //       connectPgSql.query(
  //         "insert into users values($1,$2,$3,now(),now(),$4,$5)",
  //         [
  //           req.body.name,
  //           (Math.random() + "").split(".")[1],
  //           (Math.random() + "").split(".")[1],
  //           req.body.password.slice(5, 10) +
  //           (Math.random() + "").split(".")[1],
  //           req.body.password
  //         ],
  //         function(err, data2) {
  //           if (err) {
  //             res.send({ errorCode: 501 });
  //             return console.log(err);
  //           }
  //           const fs=require('fs');
  //           fs.mkdir('./public/img/'+(req.body.password.slice(10, 15) +
  //           (Math.random() + "").split(".")[1]),(err)=>{
  //             if(err)return res.send({ errorCode: 1001 });
  //           })
  //           connectPgSql.query(
  //             "select * from users where user_name=$1",
  //             [req.body.name],
  //             function(err, data3) {
  //               if (err) {
  //                 res.send({ errorCode: 501 });
  //                 return console.log(err);
  //               }
  //               res.send({ errorCode: 0, data: data3.rows[0] });
  //             }
  //           );
  //         }
  //       );
  //     }
  //   }
  // );
});

module.exports = router;
